package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.adapter.ScoreInfoAdapter;
import graduate.cslg.com.universityshopping.entity.ScoreInfoBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ScoreActivity extends AppCompatActivity {

    private TextView scoreText;
    private TextView countTip;
    private LinearLayout tipLayout;
    private RecyclerView mRecyclerView;
    private List<ScoreInfoBean> mScoreInfoBeanList;
    private String getUserAccount;
    private ScoreInfoAdapter mInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");
        initViews();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        getScoreInfo();
    }

    private void initViews() {
        scoreText = findViewById(R.id.stu_score);
        countTip = findViewById(R.id.count_tip);
        tipLayout = findViewById(R.id.tip_layout);
        mRecyclerView = findViewById(R.id.score_recycler);
    }

    private void getScoreInfo() {
        RetrofitDevelop.getTestService()
                .getDorScoreInfo(getUserAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ScoreInfoBean>>() {
                    @Override
                    public void onCompleted() {
                        int n = mScoreInfoBeanList.size();
                        if (n > 0) {
                            int sum = 100;
                            for (int i = 0; i < n; i++) {
                                sum = sum - mScoreInfoBeanList.get(i).getScore_count();
                            }
                            scoreText.setText(String.valueOf(sum) + "分");
                            mInfoAdapter = new ScoreInfoAdapter(mScoreInfoBeanList);
                            tipLayout.setVisibility(View.VISIBLE);
                            mRecyclerView.setAdapter(mInfoAdapter);
                        } else {
                            scoreText.setText("100分");
                            countTip.setText("您目前还没有扣分情况");
                            tipLayout.setVisibility(View.INVISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<ScoreInfoBean> scoreInfoBeans) {
                        mScoreInfoBeanList = scoreInfoBeans;
                    }
                });
    }
}
